<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="parentName">
        <el-input
          v-model="queryParams.parentName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="parentEmail">
        <el-input
          v-model="queryParams.parentEmail"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['parentProfile:parentProfile:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['parentProfile:parentProfile:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['parentProfile:parentProfile:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['parentProfile:parentProfile:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parentProfileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="parentId" />
      <el-table-column label="头像" align="center" prop="parentavatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.parentavatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="姓名" align="center" prop="parentName" />
      <el-table-column label="角色标识" align="center" prop="roleName" />
      <el-table-column label="电话" align="center" prop="parentPhoneNumber" />
      <el-table-column label="邮箱" align="center" prop="parentEmail" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['parentProfile:parentProfile:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['parentProfile:parentProfile:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改家长信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="头像" prop="parentavatar">
          <image-upload v-model="form.parentavatar"/>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="电话" prop="parentPhoneNumber">
          <el-input v-model="form.parentPhoneNumber" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="邮箱" prop="parentEmail">
          <el-input v-model="form.parentEmail" placeholder="请输入邮箱" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParentProfile, getParentProfile, delParentProfile, addParentProfile, updateParentProfile } from "@/api/parentProfile/parentProfile";

export default {
  name: "ParentProfile",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家长信息表格数据
      parentProfileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        parentName: null,
        parentPhoneNumber: null,
        parentEmail: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // parentavatar: [
        //   { required: true, message: "头像不能为空", trigger: "blur" }
        // ],
        userName: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        parentName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        // parentPhoneNumber: [
        //   { required: true, message: "电话不能为空", trigger: "blur" }
        // ],
        // parentEmail: [
        //   { required: true, message: "邮箱不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询家长信息列表 */
    getList() {
      this.loading = true;
      listParentProfile(this.queryParams).then(response => {
        this.parentProfileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        parentId: null,
        userId: null,
        parentavatar: null,
        userName: null,
        parentName: null,
        roleName: null,
        parentPhoneNumber: null,
        parentEmail: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.parentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加家长信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const parentId = row.parentId || this.ids
      getParentProfile(parentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改家长信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.parentId != null) {
            updateParentProfile(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParentProfile(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const parentIds = row.parentId || this.ids;
      this.$modal.confirm('是否确认删除家长信息编号为"' + parentIds + '"的数据项？').then(function() {
        return delParentProfile(parentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('parentProfile/parentProfile/export', {
        ...this.queryParams
      }, `parentProfile_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
@keyframes fadeInUp {
  0% { opacity: 0; transform: translateY(40px); }
  100% { opacity: 1; transform: translateY(0); }
}
.app-container {
  padding: 36px 0 36px 0;
  min-height: 100vh;
  background: linear-gradient(120deg, #f6f8fc 0%, #f1f4f9 100%);
  animation: fadeInUp 0.7s cubic-bezier(0.23, 1, 0.32, 1);
}
.app-container .el-form {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 28px 28px 10px 28px;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(64, 158, 255, 0.10), 0 1.5px 8px rgba(230,30,30,0.04);
  border: 1.5px solid #f0f2f5;
  margin-bottom: 28px;
  animation: fadeInUp 0.8s 0.1s backwards;
}
.app-container .el-form .el-form-item {
  margin-bottom: 0;
  margin-right: 20px;
}
.app-container .el-form .el-form-item .el-input__inner,
.app-container .el-form .el-form-item .el-select .el-input__inner {
  border-radius: 8px;
  border-color: #dcdfe6;
  transition: all 0.3s ease;
}
.app-container .el-form .el-form-item .el-input__inner:hover,
.app-container .el-form .el-form-item .el-input__inner:focus,
.app-container .el-form .el-form-item .el-select .el-input__inner:hover,
.app-container .el-form .el-form-item .el-select .el-input__inner:focus {
  border-color: #1e8ce6;
}
.app-container .el-form .el-form-item .el-button {
  padding: 8px 20px;
  border-radius: 8px;
  transition: all 0.3s ease;
}
.app-container .el-form .el-form-item .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.app-container .mb8 {
  margin-bottom: 28px;
}
.app-container .mb8 .el-button {
  padding: 10px 20px;
  border-radius: 8px;
  transition: all 0.3s ease;
  font-weight: 500;
}
.app-container .mb8 .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.app-container .mb8 .el-button.el-button--primary {
  background: linear-gradient(135deg, #f5e1e1, #d77c7e);
  border: none;
}
.app-container .mb8 .el-button.el-button--primary:hover {
  background: linear-gradient(135deg, #ff6b6b, #e61e1e);
}
.app-container .el-table {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(64, 158, 255, 0.10), 0 1.5px 8px rgba(230,30,30,0.04);
  border: 1.5px solid #f0f2f5;
  margin-bottom: 28px;
  animation: fadeInUp 0.9s 0.2s backwards;
}
.app-container .el-table::before {
  display: none;
}
.app-container .el-table .el-table__header-wrapper th {
  background: rgba(230, 30, 30, 0.05);
  color: #2c3e50;
  font-weight: 600;
  padding: 15px 0;
}
.app-container .el-table .el-table__body-wrapper td {
  padding: 15px 0;
  color: #606266;
  transition: background 0.2s;
}
.app-container .el-table .el-table__body-wrapper tr:hover td {
  background: #f6f8fc;
}
.app-container .el-table .el-button {
  padding: 6px 12px;
  border-radius: 6px;
  transition: all 0.3s ease;
}
.app-container .el-table .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.app-container .el-table .el-button.el-button--danger {
  background: linear-gradient(135deg, #ff4d4f, #ff7875);
  border: none;
}
.app-container .el-table .el-button.el-button--danger:hover {
  background: linear-gradient(135deg, #ff7875, #ff4d4f);
}
.app-container .el-pagination {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 20px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(64, 158, 255, 0.10), 0 1.5px 8px rgba(230,30,30,0.04);
  border: 1.5px solid #f0f2f5;
  text-align: center;
  animation: fadeInUp 1s 0.3s backwards;
}
.app-container .el-pagination .el-pagination__total,
.app-container .el-pagination .el-pagination__jump {
  color: #606266;
}
.app-container .el-pagination .btn-prev,
.app-container .el-pagination .btn-next,
.app-container .el-pagination .el-pager li {
  background: transparent;
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  transition: all 0.3s ease;
}
.app-container .el-pagination .btn-prev:hover,
.app-container .el-pagination .btn-next:hover,
.app-container .el-pagination .el-pager li:hover {
  color: #e61e1e;
  border-color: #e61e1e;
}
.app-container .el-pagination .btn-prev.active,
.app-container .el-pagination .btn-next.active,
.app-container .el-pagination .el-pager li.active {
  background: #e61e1e;
  color: white;
  border-color: #e61e1e;
}
.app-container .el-dialog {
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  animation: fadeInUp 0.7s;
}
.app-container .el-dialog .el-dialog__header {
  background: linear-gradient(135deg, #e61e1e, #ff6b6b);
  padding: 20px;
  margin: 0;
}
.app-container .el-dialog .el-dialog__header .el-dialog__title {
  color: white;
  font-size: 18px;
  font-weight: 600;
}
.app-container .el-dialog .el-dialog__header .el-dialog__headerbtn {
  top: 20px;
}
.app-container .el-dialog .el-dialog__header .el-dialog__headerbtn .el-icon-close {
  color: white;
}
.app-container .el-dialog .el-dialog__body {
  padding: 30px;
}
.app-container .el-dialog .el-dialog__body .el-form-item__label {
  font-weight: 500;
  color: #2c3e50;
}
.app-container .el-dialog .el-dialog__body .el-input__inner,
.app-container .el-dialog .el-dialog__body .el-textarea__inner {
  border-radius: 8px;
  border-color: #dcdfe6;
  transition: all 0.3s ease;
}
.app-container .el-dialog .el-dialog__body .el-input__inner:hover,
.app-container .el-dialog .el-dialog__body .el-input__inner:focus,
.app-container .el-dialog .el-dialog__body .el-textarea__inner:hover,
.app-container .el-dialog .el-dialog__body .el-textarea__inner:focus {
  border-color: #1692a5;
}
.app-container .el-dialog .el-dialog__footer {
  padding: 20px 30px;
  border-top: 1px solid #ebeef5;
}
.app-container .el-dialog .el-dialog__footer .el-button {
  padding: 10px 25px;
  border-radius: 8px;
  transition: all 0.3s ease;
}
.app-container .el-dialog .el-dialog__footer .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
</style>
